<template>
  <div>
    <el-dialog
      title="新增文章"
      :visible="addDialogVisible"
      width="800px"
      @close="handelClose"
    >
      <el-form ref="formDate" :model="formDate" :rules="rules" :inline="true">
        <el-form-item label="文章标题" prop="title" style="display: block">
          <el-input v-model="formDate.title"></el-input>
        </el-form-item>
        <el-form-item
          label="文章内容"
          prop="articleBody"
          style="display: block"
        >
          <quillEditor v-model="formDate.articleBody" :options="editorOptions"></quillEditor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL" style="display: block">
          <el-input v-model="formDate.videoURL"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handelClose">取 消</el-button>
        <el-button type="primary" @click="addArticle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'AddArticle',
  props: {
    addDialogVisible: Boolean,
    id: {
      type: Number,
      default: null
    }
  },
  components: {
    quillEditor
  },
  data () {
    return {
      formDate: {
        title: '',
        articleBody: '',
        videoURL: null,
        id: null
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'change' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  methods: {
    handelClose () {
      // 1 关闭弹窗
      this.$emit('update:addDialogVisible', false)
      // 2 清除表单验证
      this.$refs.formDate.resetFields()
      // 3 清除表单内容
      this.formDate = {
        title: '',
        articleBody: '',
        videoURL: null
      }
    },
    async addArticle () {
      // 1 校验表单
      this.$refs.formDate.validate()
      // 2调用接口，看调用的是新增，还是修改
      // 修改把id赋值上去
      this.formDate.id = this.id
      this.formDate.id ? await update(this.formDate) : await add(this.formDate)
      // 3 更新父组件
      this.$parent.getArticleList()
      // 4关闭弹层
      this.handelClose()
      this.$message.success('操作成功')
    }
  }
}
</script>
<style scoped lang='less'>
.el-dialog {
  border-radius: 10px;
}
::v-deep .el-input .el-input__inner {
  height: 36px;
  line-height: 36px;
}
::v-deep .el-input {
  width: 600px;
}
::v-deep .el-textarea__inner {
  width: 600px;
  height: 235px;
}
::v-deep .quill-editor {
  width: 600px;
  height: 235px;
}
</style>
